package com.bytedance.scenedemo.ability_compatibility.scene_result;

import com.bytedance.scene.ui.SceneNavigator;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;


/**
 * Ability 启动 Scene（托管到SceneContainerAbility）
 *
 * @author JiangQi
 * @since 2018-03-08
 */
public class AbilityGetSceneResultSampleAbility extends Ability {

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_fraction_getScene_result_sample_ability);
        getWindow().setStatusBarColor(Color.getIntColor("#404FB5"));

        initListener();
    }

    private void initListener() {
        Button button = (Button) findComponentById(ResourceTable.Id_button_ability_get_scene_result1);
        button.setClickedListener(component -> {
            new SceneNavigator(this, 0).startSceneForResult(TestScene2.class, null, result -> {
                System.out.println("TAG----"+result.toString());
                showToast(result.toString());
            });
        });
    }

    /**
     * Toast弹框
     *
     * @param content 内容
     */
    protected void showToast(String content) {
        Component toastLayout = LayoutScatter.getInstance(getContext())
                .parse(com.bytedance.scene.ResourceTable.Layout_layout_toast, null, false);
        Text toastText = (Text) toastLayout.findComponentById(com.bytedance.scene.ResourceTable.Id_text_msg_toast);
        toastText.setText(content);
        new ToastDialog(getContext())
                .setComponent(toastLayout)
                .setTransparent(true)
                .show();
    }
}
